/*===============================
 *Title      :  MediaCursorAdapter.java
 *Created  :[ 2012-3-23 下午1:28:19 ] by fanlt
 * ==============================
 * Description:   
 * Copyright  :  Copyright (c) 2012
 * Company    :  Flyfot
 * @author  <a href="fanlt@flyfot.cn">fanlt</a>
 *
 */
package org.zxstudio.encrypt;

import java.io.File;
import java.util.ArrayList;

import org.zxstudio.encrypt.MediaBaseAdapter.onItemSelectedListener;
import org.zxstudio.encrypt.utils.LazyImageLoader;
import org.zxstudio.encrypt.utils.RLog;

import android.content.Context;
import android.database.Cursor;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * @author fan
 * 
 */
public class MediaCursorAdapter extends CursorAdapter {

	private static final String TAG = "MediaCursorAdapter";
	private static final boolean DEBUG = true;

	public static final int _ID = 0;
	public static final int TITLE = 1;
	public static final int DATA = 2;

	private Cursor mCursor;
	private Context mContext;
	private LazyImageLoader mLoader;
	private int mMediaType;

	private ArrayList<Long> mSelectFiles = new ArrayList<Long>();
	private onItemSelectedListener mSelectedListener;

	/**
	 * @param context
	 * @param c
	 */
	public MediaCursorAdapter(Context context, Cursor c) {
		super(context, c);
		mCursor = c;
		mContext = context;
		mLoader = new LazyImageLoader();
	}

	public void setMediaType(int type) {
		mMediaType = type;
	}

	public void updateSelItem(View view ,long id) {
		if (mSelectFiles.contains(id)) {
			mSelectFiles.remove(id);
			view.findViewById(R.id.check_icon).setBackgroundResource(R.drawable.btn_check_buttonless_off);
		} else {
			mSelectFiles.add(id);
			view.findViewById(R.id.check_icon).setBackgroundResource(R.drawable.btn_check_buttonless_on);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.widget.CursorAdapter#bindView(android.view.View,
	 * android.content.Context, android.database.Cursor)
	 */
	@Override
	public void bindView(View view, Context context, Cursor cursor) {
		final long id = cursor.getLong(_ID);
		String name = cursor.getString(TITLE);
		final String path = cursor.getString(DATA);

		ImageView iconView = (ImageView) view
				.findViewById(R.id.gridview_fileicon);
		TextView nameView = (TextView) view
				.findViewById(R.id.gridview_filename);
		ImageView selectView = (ImageView) view.findViewById(R.id.check_icon);
		if (name == null) {
			File file = new File(path);
			if (file.exists()) {
				name = file.getName();
			}
		}
		nameView.setText(name);
		RLog.v(TAG, "bindView :name :" + name);
		if (mMediaType == MediaBean.IMAGE) {
			iconView.setImageResource(R.drawable.icon_image);
			mLoader.loadImage(iconView, path, mLoaderListener);

		} else if (mMediaType == MediaBean.VIDEO) {
			iconView.setImageResource(R.drawable.icon_video);
			mLoader.loadImage(iconView, path, mLoaderListener);
		} else {
			iconView.setImageResource(R.drawable.icon_audio);
		}

		if (mSelectFiles.contains(id)) {
			selectView
					.setBackgroundResource(R.drawable.btn_check_buttonless_on);
		} else {
			selectView
					.setBackgroundResource(R.drawable.btn_check_buttonless_off);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.widget.CursorAdapter#newView(android.content.Context,
	 * android.database.Cursor, android.view.ViewGroup)
	 */
	@Override
	public View newView(Context context, Cursor cursor, ViewGroup parent) {

		return LayoutInflater.from(mContext).inflate(R.layout.item_gridview,
				null);
	}

	private LazyImageLoader.OnImageLoaderListener mLoaderListener = new LazyImageLoader.OnImageLoaderListener() {

		@Override
		public void onImageLoad(ImageView t, Drawable drawable, int mimeType) {
			if (DEBUG) {
				Log.v(TAG, "onImageLoad" + mimeType);
			}
			if (mimeType != MediaBean.AUDIO) {
				t.setImageDrawable(drawable);
			} else {
				t.setImageResource(R.drawable.icon_audio);
			}

		}

		@Override
		public void onError(ImageView t) {
			if (DEBUG) {
				Log.v(TAG, "onError....................");
			}
		}

		@Override
		public void onImageLoad(ImageView t, Drawable drawable) {
			if (drawable != null)
				t.setImageDrawable(drawable);
		}

	};
	
	public interface onItemSelectedListener {
		public void hasItemSelected();

		public void noItemSelected();
	}
	
	public void registerItemSelectListener(onItemSelectedListener listener) {
		mSelectedListener = listener;
	}

	public void unregisterItemSelectListener() {
		mSelectedListener = null;
	}
}
